<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://unpkg.com/vue@next"></script>
    <script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js"></script>
    <script src="https://unpkg.com/vuex@next"></script>
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script>
      // import {createApp} from 'vue'
      // import {createStore} from 'vuex'
      const { createApp } = Vue;
      const { createStore, mapActions } = Vuex;

      const getData = () => {
        return new Promise((resolve) => {
          setTimeout(() => {
            console.log("getData....");
            resolve();
          }, 1000);
        });
      };

      const getOtherData = () => {
        return new Promise((resolve) => {
          setTimeout(() => {
            console.log("getOtherData....");
            resolve();
          }, 1000);
        });
      };

      const store = createStore({
        state: {
          count: 0,
        },
        mutations: {
          increment(state) {
            console.log('increment...')
            state.count++;
          },
        },
        actions: {
          increment(context) {
            context.commit("increment");
          },
          incrementAsync({ commit }, payload) {
            console.log("incrementAsync:", payload);
            setTimeout(() => {
              commit("increment");
            }, 1000);
          },
          // Composing Actions
          // actionA({ commit }) {
          //   console.log('actionA:')
          //   return new Promise((resolve) => {
          //     setTimeout(() => {
          //       commit("increment");
          //       resolve();
          //     }, 1000);
          //   });
          // },
          // actionB({ commit, dispatch }) {
          //   console.log('actionB:')
          //   // 先完成A 然后完成increment
          //   dispatch("actionA").then(() => {
          //     commit("increment");
          //   });
          // },
          async actionA({commit}) {
            commit('increment',await getData())
          },
          async actionB ({dispatch,commit}) {
            await dispatch('actionA')
            commit('increment',await getOtherData())
          }
        },
      });

      const app = createApp({
        template: `
            <div>{{ $store.state.count }}</div>
            <button @click="$store.dispatch('increment')">increment</button>
            <button @click="$store.dispatch('incrementAsync',{amount: 100})">incrementAsync</button>
            <button @click="$store.dispatch({type: 'incrementAsync',amount: 100})">incrementAsync</button>
            <button @click="$store.dispatch({type: 'incrementAsync',amount: 100})">incrementAsync</button>
            <button @click="increment()">increment()</button>
            <button @click="add()">add()</button>
            <button @click="$store.dispatch('actionA')">actionA()</button>
            <button @click="$store.dispatch('actionB')">actionB()</button>
        `,
        methods: {
          ...mapActions([
            "increment", // map `this.increment()` to `this.$store.dispatch('increment')`
          ]),
          ...mapActions({
            add: "increment", // map `this.add()` to `this.$store.dispatch('increment')`
          }),
        },
      });
      app.use(store);
      app.mount("#app");
    </script>
  </body>
</html>
